var http        =       require('http');
var fs          =       require('fs');
var cheerio     =       require('cheerio');
var request     =       require('request').defaults({maxRedirects:100});

var url = "https://www.zhihu.com/question/";

function fetchPage(target){
    var id = 51190538;
    //startRequest(target+id)
    while (id<1000000000) {
        startRequest(target+id);
        function sleep(milliSeconds) {
            var startTime = new Date().getTime();
            while (new Date().getTime() < startTime + milliSeconds);
        };
        sleep(1000);  //等待10秒。
        id++;
    }
}

function startRequest(target) {
    console.log(target);
    request({
        url:target,
        gzip:true
        },
        function (error, response, body) {
            console.log("res");
            if (!error && response.statusCode == 200) {
                var $ = cheerio.load(body);
                var item = {
                    title : $('ul ').children('title').text(), //标题
                    ans_num : $('#zh-question-answer-num').text(), //答案数目
                    com_num : $('#z-icon-comment').text(), //评论数
                    focus_num : $('#zh-question-side-header-wrap').remove('button').text() //关注数
                }
                console.log(item);
            }
    })
}

fetchPage(url);      //主程序开始运行
